import Vue from 'vue'
import Vuex from 'vuex'
import { userInfoApi } from '@/api'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token')
    token: '',
    userInfo: {}
  },
  mutations: {
    setToken: function (state, token) {
      state.token = token
      // localStorage.setItem('token', token)
    },
    setUser (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async userInfoHandle (context) {
      const res = await userInfoApi().catch((err) => err)
      console.log(res, 4)
      context.commit('setUser', res.data.data)
    }
  },
  modules: {}
})
